package com.example.demo.lab2.controller;

import com.example.demo.lab2.entity.DepWithEmp;
import com.example.demo.lab2.entity.Departments;
import com.example.demo.lab2.service.DepartmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


@Controller
@RequestMapping("v1")
public class DepartmentsController {

    @Autowired
    private DepartmentsService departmentsService;

    @GetMapping("/dep/list")
    public ResponseEntity<?> list() {
        List<Departments> departmentsList = departmentsService.list();
        return ResponseEntity.ok(departmentsList);
    }

    @PutMapping("/dep/insert")
    public ResponseEntity<?> insert(Departments departments) {
        int insert = departmentsService.insert(departments);
        return ResponseEntity.ok(insert);
    }

    @DeleteMapping("/dep/delete")
    public ResponseEntity<?> delete(BigDecimal id) {
        int delete = departmentsService.delete(id);
        return ResponseEntity.ok(delete);
    }

    @PostMapping("/dep/update")
    public ResponseEntity<?> update(Departments departments) {
        int update = departmentsService.update(departments);
        return ResponseEntity.ok(update);
    }

    //1对多
    @PostMapping("/dep/listWithEmp")
    public ResponseEntity<?> listWithEmp(){
        List<DepWithEmp> depWithEmps = departmentsService.listWithEmp();
        return ResponseEntity.ok(depWithEmps);
    }
}
